<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		
	$model=new Users();
	$model=Users::model()->find('LOWER(login_name)=? and password=md5(concat(?,salt))',array(strtolower($this->username),md5($this->password)));
//	$model=Users::model()->find("login_name=?",array($this->username));
		if($model==null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if($model->password!==md5(md5($this->password).$model->salt))
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else
			{
				$this->errorCode=self::ERROR_NONE;
				Yii::app()->session['username']=$this->username;
				Yii::app()->session['password']=$this->password;
				Yii::app()->session['email']=$model->email;
				Yii::app()->session['user_id']=$model->getAttribute('user_id');
				//print_r(Yii::app()->session->sessionID);exit();
			}
		return !$this->errorCode;
	}
	//register返回三种三个字符串对应三种情况	
	static function register($login_name,$password,$email)
	{
		$str='error';
		
		if ($login_name!='' && $password!='' && $email!='')
		{
		$model=new Users();
		$model->login_name=$login_name;
		$model->salt=Func::generate_salt();
		$model->password=md5((md5($password)).$model->salt);
		$model->email=$email;
		//验证用户名可用
		$user=Users::model ()->find ( "login_name=?", array ($model->login_name));
		if (Users::model ()->find ( "login_name=?", array ($model->login_name) ) != null)
		{
			$str= "user is exist!!";
		}
		 else 
		 {
			$model->save ();
		//验证注册是否成功 
		 	$users=new Users();
			$users= Users::model ()->find ( "login_name=?", array ($model->login_name) );
	$str=$users==''?'register false':'register success';
		 }
		}
	return $str;
	}
}